package com.game.view.layer.gameMap
{
	import com.game.manager.GameSceneManager;
	import com.game.view.layer.LayerBase;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	public class GameMapBase extends LayerBase
	{
		public function GameMapBase()
		{
			super();
			tabChildren = false;
			tabEnabled = false;
		}
		
		protected static var MAP_INDEX:Object = {MIN:0,MAX:0};
		
		protected function setMapIndex(min:int = 0,max:int = 0):void
		{
			MAP_INDEX = {MIN:min,MAX:max};
		}
		
		protected function mOver(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.BUTTON;
			param1.currentTarget.gotoAndStop(3);
		}
		
		protected function mOut(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.AUTO;
			var str1:String = param1.currentTarget.name;
			if (!hasOwnProperty(str1) || this[str1] == null)
			{
				return;
			}
			if (str1 == "s" + gc.userData.maxStage + "_" + gc.userData.maxLevel)
			{
				this[str1].gotoAndStop(2);
			}
			else
			{
				this[str1].gotoAndStop(1);
			}
		}
		
		protected function onSelected(param1:MouseEvent):void
		{
			Mouse.cursor = MouseCursor.AUTO;
			GameSceneManager.getIns().switchScene(GameSceneManager.START_FIGHTING,param1.currentTarget.name.replace("s",""));
		}
		
		protected function init():void
		{
			for (var stageNum:int = MAP_INDEX.MIN; stageNum <= MAP_INDEX.MAX; stageNum++)
			{
				levelNum = 0;
				for (var levelNum:int = 1; levelNum <= 3; levelNum++)
				{
					if (this["s" + stageNum + "_" + levelNum] != null)
					{
						this["s" + stageNum + "_" + levelNum].gotoAndStop(1);
						this["s" + stageNum + "_" + levelNum].buttonMode = true;
					}
				}
			}
		}
		
		override protected function onAdded():void
		{
			init();
			var bStage:int = gc.userData.maxStage;
			var bLevel:int;
			if (bStage > MAP_INDEX.MAX)
			{
				bStage = MAP_INDEX.MAX;
			}
			for (var i:int = MAP_INDEX.MIN; i <= bStage; i++)
			{
				var j:int;
				if (i < bStage)
				{
					bLevel = 3;
				}
				else
				{
					bLevel = gc.userData.maxLevel;
				}
				for (j = 1; j <= bLevel; j++)
				{
					var str1:String = "s" + i + "_" + j;
					if (this[str1] != null)
					{
						setActivate(this[str1] as MovieClip);
						this[str1].addEventListener(MouseEvent.CLICK,onSelected);
					}
				}
			}
			var str2:String = "s" + gc.userData.maxStage + "_" + gc.userData.maxLevel;
			if (hasOwnProperty(str2) && this[str2] != null)
			{
				this[str2].gotoAndStop(2);
			}
			var menu:MapMenu = getChildByName("showedMapMenu") as MapMenu;
			if (menu != null)
			{
				removeChild(menu);
			}
			menu = new MapMenu();
			menu.x = menu.y = 0;
			menu.name = "showedMapMenu";
			addChild(menu);
		}
		
		override protected function onRemoved():void
		{
			for (var i:int = MAP_INDEX.MIN; i <= MAP_INDEX.MAX; i++)
			{
				for (var j:int = 0; j <= 3; j++)
				{
					var str1:String = "s" + i + "_" + j;
					if (hasOwnProperty(str1) && this[str1] != null)
					{
						setDeactivate(this[str1] as MovieClip);
						this[str1].removeEventListener(MouseEvent.CLICK,onSelected);
					}
				}
			}
		}
		
		protected function setActivate(mc:MovieClip):void
		{
			mc.addEventListener(MouseEvent.ROLL_OVER, mOver);
			mc.addEventListener(MouseEvent.ROLL_OUT, mOut);
			mc.buttonMode = true;
		}
		
		protected function setDeactivate(mc:MovieClip):void
		{
			mc.removeEventListener(MouseEvent.ROLL_OVER, mOver);
			mc.removeEventListener(MouseEvent.ROLL_OUT, mOut);
			mc.buttonMode = false;
		}
	}
}
